package com.example.backend.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.backend.domain.entity.RestBean;
import com.example.backend.domain.po.Schedules;
import com.example.backend.service.ISchedulesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dyy
 * @since 2025-05-27
 */
@RestController
@RequestMapping("/schedules")
public class SchedulesController {
    @Autowired
    private ISchedulesService schedulesService;

    // 根据 ID 查询单个工作记录
    @GetMapping("/{id}")
    public RestBean<Schedules> getScheduleById(@PathVariable Integer id) {
        Schedules schedule = schedulesService.getById(id);
        return RestBean.success(schedule);
    }

    // 根据多个 ID 查询工作记录
    @GetMapping("/batch")
    public RestBean<List<Schedules>> getSchedulesByIds(@RequestParam List<Integer> ids) {
        List<Schedules> schedules = schedulesService.listByIds(ids);
        return RestBean.success(schedules);
    }

    // 根据条件查询工作记录
    @GetMapping("/search")
    public RestBean<List<Schedules>> searchSchedules(
            @RequestParam(required = false) Integer employeeId,
            @RequestParam(required = false) String status) {
        LambdaQueryWrapper<Schedules> queryWrapper = new LambdaQueryWrapper<>();
        if (employeeId != null) {
            queryWrapper.eq(Schedules::getEmployeeId, employeeId);
        }
        if (status != null && !status.isEmpty()) {
            queryWrapper.eq(Schedules::getStatus, status);
        }
        List<Schedules> schedules = schedulesService.list(queryWrapper);
        return RestBean.success(schedules);
    }

    // 分页查询工作记录
    @GetMapping("/page")
    public RestBean<IPage<Schedules>> getSchedulesByPage(
            @RequestParam Integer current,
            @RequestParam Integer size,
            @RequestParam(required = false) Integer employeeId,
            @RequestParam(required = false) String status) {
        Page<Schedules> page = new Page<>(current, size);
        LambdaQueryWrapper<Schedules> queryWrapper = new LambdaQueryWrapper<>();
        if (employeeId != null) {
            queryWrapper.eq(Schedules::getEmployeeId, employeeId);
        }
        if (status != null && !status.isEmpty()) {
            queryWrapper.eq(Schedules::getStatus, status);
        }
        IPage<Schedules> schedulePage = schedulesService.page(page, queryWrapper);
        return RestBean.success(schedulePage);
    }

    // 新增工作记录
    @PostMapping
    public RestBean<Void> addSchedule(@RequestBody Schedules schedule) {
        boolean save = schedulesService.save(schedule);
        return RestBean.success();
    }

    // 更新工作记录信息
    @PutMapping("/{id}")
    public RestBean<Void> updateSchedule(
            @PathVariable Integer id,
            @RequestBody Schedules schedule) {
        schedule.setId(id);
        boolean update = schedulesService.updateById(schedule);
        return RestBean.success();
    }

    // 删除工作记录
    @DeleteMapping("/{id}")
    public RestBean<Void> deleteSchedule(@PathVariable Integer id) {
        boolean remove = schedulesService.removeById(id);
        return RestBean.success();
    }
}
